package org.geektimes.configuration.microprofile.source;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/3/19 8:44
 */
public class DefaultResourceConfigSource extends MapBesedConfigSource {
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    private final String PATH_NAME = "META-INF/config.properties";

    public DefaultResourceConfigSource() throws IOException {
        super("Default Config File", 100);
    }


    @Override
    protected void prepareConfigData(Map configData) throws Throwable {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(PATH_NAME);
        if(resource == null){
            logger.info("The default config file can't be found in the classpath :" + PATH_NAME);
            return;
        }

        try(InputStream inputStream = resource.openStream()){
            Properties properties = new Properties();
            properties.load(inputStream);
            configData.putAll(properties);
        }
    }
}
